package com.itheima.response;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

@WebServlet("/demo12")
public class ServletDemo12 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //利用网络字节输出流，将二进制写出到浏览器端
        /*String path = getServletContext().getRealPath("/1.jpg");
        FileInputStream fis = new FileInputStream(path);*/
        InputStream fis = getServletContext().getResourceAsStream("/1.jpg");
        ServletOutputStream os = resp.getOutputStream();
        byte[] bytes = new byte[1024];
        int len =-1;
        while((len=fis.read(bytes))!=-1){
            os.write(bytes,0,len);
        }
        fis.close();


    }
}
